package com.migo.service.impl;

import com.migo.defence.RtCode;
import com.migo.entity.PvMgFirmwareEntity;
import com.migo.service.PvMgFirmwareService;
import com.migo.utils.R;
import com.migo.utils.redis.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import com.migo.dao.PvMgFirmwareVerisonDao;
import com.migo.entity.PvMgFirmwareVerisonEntity;
import com.migo.service.PvMgFirmwareVerisonService;



@Service("pvMgFirmwareVerisonService")
public class PvMgFirmwareVerisonServiceImpl implements PvMgFirmwareVerisonService {
	@Autowired
	private PvMgFirmwareVerisonDao pvMgFirmwareVerisonDao;

	@Autowired
	private PvMgFirmwareService pvMgFirmwareService;

	@Autowired
	private RedisCacheUtil<PvMgFirmwareEntity> pvMgFirmwareLastRedisCacheUtil;

	@Autowired
	private RedisCacheUtil<PvMgFirmwareVerisonEntity> pvMgFirmwareVersionLastRedisCacheUtil;

	@Autowired
	public RedisTemplate redisTemplate;



	@Override
	public PvMgFirmwareVerisonEntity queryObject(Integer id){
		return pvMgFirmwareVerisonDao.queryObject(id);
	}
	
	@Override
	public List<PvMgFirmwareVerisonEntity> queryList(Map<String, Object> map){
		return pvMgFirmwareVerisonDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return pvMgFirmwareVerisonDao.queryTotal(map);
	}
	
	@Override
	public void save(PvMgFirmwareVerisonEntity pvMgFirmwareVerison){
		pvMgFirmwareVerisonDao.save(pvMgFirmwareVerison);
	}
	
	@Override
	public void update(PvMgFirmwareVerisonEntity pvMgFirmwareVerison){
		pvMgFirmwareVerisonDao.update(pvMgFirmwareVerison);
	}
	
	@Override
	public void delete(Integer id){
		pvMgFirmwareVerisonDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		pvMgFirmwareVerisonDao.deleteBatch(ids);
	}

	@Override
	public PvMgFirmwareVerisonEntity queryMatchBigVision(String app_vision) {
		PvMgFirmwareVerisonEntity bigVersion = null;
		Map<String, Object> map=new HashMap<String, Object>();
		if(!"".equals(app_vision) && app_vision!=null ){
			String[] ArrayTmp=app_vision.split("\\.");
			map.put("app_vision01",ArrayTmp[0]);
			map.put("app_vision02",ArrayTmp[1]);
			map.put("app_vision03",ArrayTmp[2]);
			bigVersion = pvMgFirmwareVerisonDao.queryMatchBigVision(map);

		}else{
			return null;
		}

		return bigVersion;
	}

	@Override
	public PvMgFirmwareVerisonEntity queryByBigVersion(String big_version) {

		return pvMgFirmwareVerisonDao.queryByBigVersion(big_version);
	}

	@Override
	public PvMgFirmwareVerisonEntity queryLastVersion(String gm_version,String pd_version,String bt_version,String pdrs_version) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("gm_version",gm_version);
		map.put("pd_version",pd_version);
		map.put("bt_version",bt_version);
		map.put("pdrs_version",pdrs_version);
		return pvMgFirmwareVerisonDao.queryLastVersion(map);
	}

	@Override
	public PvMgFirmwareVerisonEntity queryTheLatestVersion() {

		return pvMgFirmwareVerisonDao.queryTheLatestVersion();
	}

	/**
	 * 刷新Redis中的 固件缓存
	 * @return
	 */
	@Override
	public boolean flushFirmwareCache(){
		try {
			/**
			 *  固件信息
			 */
			Map<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("status","0");
			List<PvMgFirmwareVerisonEntity> firmwareVerisonList = queryList(queryParams);
			if(firmwareVerisonList.size()>0){
				String keys = "queryMatchBigVision:" + "app_version:"  + "*";
				redisTemplate.keys(keys);
				RedisConnection connection = RedisConnectionUtils.getConnection(redisTemplate.getConnectionFactory());
				Cursor<byte[]> result = connection.scan(new ScanOptions.ScanOptionsBuilder().count(10).match(keys).build());
				//cursor有id和position这两个属性，id则对应 scan cursor 的cursor的值，poisition则是当前遍历到第几个
				while (result.hasNext()) {//这里可以改用for循环来获取指定数量的key
					String key=new String(result.next());
					//对key的操作，或者先放到一个集合里面，然后再进行后续操作
					pvMgFirmwareVersionLastRedisCacheUtil.deleteObject(key);
				}
			}
		}catch (NoSuchElementException e) {
			return false;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}


		/**
		 *  固件信息
		 */
		Map<String, Object> queryFirmwareParams = new HashMap<String, Object>();
		List<PvMgFirmwareEntity> firmwareList = pvMgFirmwareService.queryList(queryFirmwareParams);
		if(firmwareList.size()>0){
			for(PvMgFirmwareEntity firmware: firmwareList){
				String key = "queryFirmwareVersionByType:" + "Type:"+firmware.getType()+":"+firmware.getVersion();
				pvMgFirmwareLastRedisCacheUtil.deleteObject(key);
				pvMgFirmwareLastRedisCacheUtil.setCacheObject(key,firmware);
			}
		}else {
			return false;
		}


		return true;
	}

}
